package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.vo.eduEnrollPlan.EduEnrollPlanKeyVO;
import com.jwsoft.manager.common.vo.eduEnrollPlan.EduEnrollPlanQueryVO;
import com.jwsoft.manager.common.vo.eduEnrollPlan.EduEnrollPlanVO;
import com.jwsoft.manager.core.dao.model.EduEnrollPlan;
import com.jwsoft.manager.core.dao.service.EduEnrollPlanService;
import com.jwsoft.manager.core.integration.EduEnrollPlanIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
/**
 * 年度招生计划表业务实现类
 *
 * @author wangtao
 * @since 2022-12-05
 */
@ApiService(funcCode = "eduEnrollPlan", title = "年度招生计划表")
public class EduEnrollPlanIntegrationImpl implements EduEnrollPlanIntegration {

    @Autowired
    public EduEnrollPlanService eduEnrollPlanService;

    @Autowired
    public EduHelper eduHelper;

    @Override
    @OpApi(funcCode = "eduEnrollPlan0001", title = "年度招生计划表分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduEnrollPlanVO> getList(EduEnrollPlanQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        if(StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(SessionUtil.getSessionInfo().getAreaCode());
        }
        if(StringUtils.isBlank(vo.getAreaCode())){
            throw  new AppException("行政区划不能为空");
        }


        if(ObjectUtils.isEmpty(vo.getYear())){
            vo.setYear(eduHelper.thisYear());
        }

        QueryWrapper<EduEnrollPlan> enrollPlanQueryWrapper = new QueryWrapper<>();
        enrollPlanQueryWrapper
                .lambda()
                .eq(!ObjectUtils.isEmpty(vo.getYear()), EduEnrollPlan::getYear, vo.getYear())
                .eq(!ObjectUtils.isEmpty(vo.getAreaCode()),EduEnrollPlan::getAreaCode,vo.getAreaCode());

        List<EduEnrollPlan> list = eduEnrollPlanService.list(enrollPlanQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduEnrollPlan> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduEnrollPlanVO> resultList = Convert.toList(EduEnrollPlanVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduEnrollPlan0002", title = "年度招生计划表根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduEnrollPlanVO getById(EduEnrollPlanKeyVO vo) {
        EduEnrollPlan entity = eduEnrollPlanService.getById(vo.getEnrollmentPlanId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduEnrollPlanVO result = Convert.convert(EduEnrollPlanVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduEnrollPlan0003", title = "年度招生计划表保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduEnrollPlanVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduEnrollPlan0004", title = "年度招生计划表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduEnrollPlan0004",
            operationName = "年度招生计划表新增",
            dataType = "enrollmentPlanId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduEnrollPlanVO add(EduEnrollPlanVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduEnrollPlan entity = Convert.convert(EduEnrollPlan.class, vo);
        eduEnrollPlanService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setEnrollmentPlanId(entity.getEnrollmentPlanId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduEnrollPlan0005", title = "年度招生计划表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduEnrollPlan0005",
            operationName = "年度招生计划表修改",
            dataType = "enrollmentPlanId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduEnrollPlanVO edit(EduEnrollPlanVO vo) {
        if (ObjectUtils.isEmpty(vo.getEnrollmentPlanId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduEnrollPlan oldEntity = eduEnrollPlanService.getById(vo.getEnrollmentPlanId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduEnrollPlan entity = Convert.convert(EduEnrollPlan.class, vo);
        eduEnrollPlanService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduEnrollPlan0006", title = "年度招生计划表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduEnrollPlan0006",
            operationName = "年度招生计划表删除",
            dataType = "enrollmentPlanId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduEnrollPlanKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getEnrollmentPlanId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduEnrollPlan entity = eduEnrollPlanService.getById(vo.getEnrollmentPlanId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduEnrollPlanService.removeById(vo.getEnrollmentPlanId());
    }

    /** 获取年度招生参数 */
    @Override
    public EduEnrollPlan getByYear(int year) {
        QueryWrapper<EduEnrollPlan> planQueryWrapper = new QueryWrapper<>();
        planQueryWrapper.lambda().eq(EduEnrollPlan::getYear, year);
        List<EduEnrollPlan> eduEnrollPlanList = eduEnrollPlanService.list(planQueryWrapper);
        if (CollectionUtils.isEmpty(eduEnrollPlanList)) {
            return null;
        }
        if (eduEnrollPlanList.size() > 1) {
            throw new AppException(year + "年度义务教育阶段招生参数配置有误：存在多条");
        }
        EduEnrollPlan eduEnrollPlan = eduEnrollPlanList.get(0);
        return eduEnrollPlan;
    }
}
